\section{Source/StringConvert.c}


\begin{footnotesize}\begin{alltt}
00001 \textcolor{preprocessor}{#include "String.h"}
00002 
00003 \textcolor{preprocessor}{#include <ctype.h>}
00004 \textcolor{preprocessor}{#include <math.h>}
00005 
00006 \textcolor{preprocessor}{#define GetCharAsInt(Int) ((Int)-'0')}
00007 \textcolor{preprocessor}{}
00008 \textcolor{keywordtype}{int} S_ToInt(S_String *String)
00009 \{
00010     \textcolor{keywordflow}{return} (\textcolor{keywordtype}{int})S_ToLong(String);
00011 \}
00012 
00013 \textcolor{keywordtype}{long} S_ToLong(S_String *String)
00014 \{
00015     \textcolor{comment}{/* Create a new string and strip the whitespace (only leading and trailing) *
      /}
00016     S_String *TempString = S\_NewStringSafe(String->String, String->Length);
00017     S\_Strip(TempString);
00018     \textcolor{keywordtype}{int} Index = 0, Order, PointPos, IsNegative = 0, ApparentLength = 0, LengthTo 
      = TempString->Length;
00019     \textcolor{keywordtype}{long} Result = 0;
00020     \textcolor{comment}{/* Check for signed-ness on the integer */}
00021     \textcolor{keywordflow}{if}(TempString->String[0] == \textcolor{charliteral}{'-'})
00022     \{
00023         Index = 1;
00024         --ApparentLength;
00025         IsNegative = 1;
00026     \}
00027     \textcolor{keywordflow}{else} \textcolor{keywordflow}{if}(TempString->String[0] == \textcolor{charliteral}{'+'})
00028     \{
00029         Index = 1;
00030         --ApparentLength;
00031     \}
00032     \textcolor{comment}{/* Check if the string has a decimal point */}
00033     \textcolor{keywordflow}{if}((PointPos = S\_FindFirstC(TempString, \textcolor{charliteral}{'.'})) != -1)
00034         LengthTo = PointPos;
00035     ApparentLength += LengthTo;
00036     \textcolor{comment}{/* Order is which 'column' the number is in */}
00037     Order = (int)pow(10, ApparentLength - 1);
00038     \textcolor{keywordflow}{while}(Index < LengthTo)
00039     \{
00040         \textcolor{keywordflow}{if}(isdigit(TempString->String[Index]))
00041         \{
00042             \textcolor{comment}{//Result += GetCharAsInt(TempString->String[Index])*Order;}
00043             Result += (IsNegative ? -GetCharAsInt(TempString->String[Index])*Orde
      r : GetCharAsInt(TempString->String[Index])*Order);
00044             Order /= 10; \textcolor{comment}{/* Only reduce the order when a number is present.}
00045 \textcolor{comment}{                            This allows for things like ',' and ' ' to be present
       in the strings */}
00046         \}
00047         ++Index;
00048     \}
00049     S\_FreeString(TempString);
00050     \textcolor{keywordflow}{return} Result;
00051 \}
\end{alltt}\end{footnotesize}
